package com.learning.platform.entity;

import jakarta.persistence.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户学习上下文实体
 * 记录用户的学习状态、偏好和历史信息
 */
@Entity
@Table(name = "user_learning_context", indexes = {
    @Index(name = "idx_user_context", columnList = "user_id"),
    @Index(name = "idx_updated_at", columnList = "updated_at")
})
public class UserLearningContext {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "user_id", nullable = false, unique = true)
    private Long userId;
    
    @Column(name = "current_language", length = 50)
    private String currentLanguage;
    
    @Column(name = "skill_level", length = 20)
    private String skillLevel; // BEGINNER, INTERMEDIATE, ADVANCED, EXPERT
    
    @Column(name = "learning_goals", length = 500)
    private String learningGoals;
    
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "recent_topics", columnDefinition = "JSON")
    private List<String> recentTopics;
    
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "learning_preferences", columnDefinition = "JSON")
    private Map<String, Object> learningPreferences;
    
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "conversation_history", columnDefinition = "JSON")
    private List<ConversationEntry> conversationHistory;
    
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "weak_areas", columnDefinition = "JSON")
    private List<String> weakAreas;
    
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "strong_areas", columnDefinition = "JSON")
    private List<String> strongAreas;
    
    @Column(name = "total_questions_asked")
    private Integer totalQuestionsAsked = 0;
    
    @Column(name = "programming_questions_asked")
    private Integer programmingQuestionsAsked = 0;
    
    @Column(name = "last_active_at")
    private LocalDateTime lastActiveAt;
    
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        createdAt = now;
        updatedAt = now;
        lastActiveAt = now;
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
        lastActiveAt = LocalDateTime.now();
    }
    
    // 构造函数
    public UserLearningContext() {}
    
    public UserLearningContext(Long userId) {
        this.userId = userId;
        this.skillLevel = "BEGINNER";
        this.totalQuestionsAsked = 0;
        this.programmingQuestionsAsked = 0;
    }
    
    // Getters and Setters
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public String getCurrentLanguage() {
        return currentLanguage;
    }
    
    public void setCurrentLanguage(String currentLanguage) {
        this.currentLanguage = currentLanguage;
    }
    
    public String getSkillLevel() {
        return skillLevel;
    }
    
    public void setSkillLevel(String skillLevel) {
        this.skillLevel = skillLevel;
    }
    
    public String getLearningGoals() {
        return learningGoals;
    }
    
    public void setLearningGoals(String learningGoals) {
        this.learningGoals = learningGoals;
    }
    
    public List<String> getRecentTopics() {
        return recentTopics;
    }
    
    public void setRecentTopics(List<String> recentTopics) {
        this.recentTopics = recentTopics;
    }
    
    public Map<String, Object> getLearningPreferences() {
        return learningPreferences;
    }
    
    public void setLearningPreferences(Map<String, Object> learningPreferences) {
        this.learningPreferences = learningPreferences;
    }
    
    public List<ConversationEntry> getConversationHistory() {
        return conversationHistory;
    }
    
    public void setConversationHistory(List<ConversationEntry> conversationHistory) {
        this.conversationHistory = conversationHistory;
    }
    
    public List<String> getWeakAreas() {
        return weakAreas;
    }
    
    public void setWeakAreas(List<String> weakAreas) {
        this.weakAreas = weakAreas;
    }
    
    public List<String> getStrongAreas() {
        return strongAreas;
    }
    
    public void setStrongAreas(List<String> strongAreas) {
        this.strongAreas = strongAreas;
    }
    
    public Integer getTotalQuestionsAsked() {
        return totalQuestionsAsked;
    }
    
    public void setTotalQuestionsAsked(Integer totalQuestionsAsked) {
        this.totalQuestionsAsked = totalQuestionsAsked;
    }
    
    public Integer getProgrammingQuestionsAsked() {
        return programmingQuestionsAsked;
    }
    
    public void setProgrammingQuestionsAsked(Integer programmingQuestionsAsked) {
        this.programmingQuestionsAsked = programmingQuestionsAsked;
    }
    
    public LocalDateTime getLastActiveAt() {
        return lastActiveAt;
    }
    
    public void setLastActiveAt(LocalDateTime lastActiveAt) {
        this.lastActiveAt = lastActiveAt;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    /**
     * 对话历史条目
     */
    public static class ConversationEntry {
        private String query;
        private String intent;
        private LocalDateTime timestamp;
        private String language;
        private String topic;
        
        public ConversationEntry() {}
        
        public ConversationEntry(String query, String intent, String language, String topic) {
            this.query = query;
            this.intent = intent;
            this.language = language;
            this.topic = topic;
            this.timestamp = LocalDateTime.now();
        }
        
        // Getters and Setters
        public String getQuery() { return query; }
        public void setQuery(String query) { this.query = query; }
        
        public String getIntent() { return intent; }
        public void setIntent(String intent) { this.intent = intent; }
        
        public LocalDateTime getTimestamp() { return timestamp; }
        public void setTimestamp(LocalDateTime timestamp) { this.timestamp = timestamp; }
        
        public String getLanguage() { return language; }
        public void setLanguage(String language) { this.language = language; }
        
        public String getTopic() { return topic; }
        public void setTopic(String topic) { this.topic = topic; }
    }
}
